<?php

/** 
 * @author Administrator
 * 
 * 
 */
class Member extends Model{
	//TODO - Insert your code here
	function __construct() {
		
		//TODO - Insert your code here
		parent::__construct();
		$this->setPrefix('m_');
		$this->setName('member');
	}
	/**
	 * 用户登录
	 * @param String $user (用户名)
	 * @param String $pwd  (密码)
	 * @return int      (1|2) 
	 */
	function userLogin($phone,$pwd){
		
		$this->setMemberTable($phone);
		
		$pwd  = md5(trim($pwd));
		$sql = "select `rand` from `".$this->_name."` where `phone` = '".$phone."'";
		
		$rs = $this->query($sql)->fetchRow();
		
		if(!$rs){
			return 1;
		}
		return 2;
		
		
	}
	function chkUserLogin($obj){
		
		//$session = Yii::app()->cache->get($this->_sessionId."");
		$session = $this->_cache->get($this->_sessionId."userInfo");
		if(!empty($_COOKIE['phone'])){
			$rs['nick']  = $_COOKIE['nick'];
			$rs['phone'] = $_COOKIE['phone'];
			$rs['email'] = '';
			Yii::app()->cache->set($this->_sessionId."userInfo",$rs,MEM_USER_LOGIN_TIME);
			$session = Yii::app()->cache->get($this->_sessionId."userInfo");
		}
		if($session['phone'] == ""){
			$url = $obj->createUrl("user/login");
			$obj->redirect($url);
			exit;
		}
		return true;
	}
	/**
	 * 
	 * 用户注册
	 * @param $phone
	 * @param $pwd
	 */
	function register($phone,$pwd){
		
		$this->setMemberTable($phone);
		$phone = trim($phone);
		
		$sql = "select count(`phone`) as total from `".$this->_name."` where `phone` ='".$phone."'";
		
		$rs = $this->query($sql)->fetchRow();
		
		if($rs['total'] > 0){
			return 3;
		}
		return 1;
	}
	/**
	 * 
	 * 修改密码
	 * @param $phone
	 * @param $pwd
	 * @param $new
	 */
	function modifyUser($phone,$password,$new){
		//$phone % 10;
		/*$this->setMemberTable($phone);
		$pwd   = md5(trim($password));
		$db = Yii::app()->db;
		$sql = "select *  from `".$this->_name."` where `phone` = '".$phone."'";
		
		$rs = $this->query($sql)->fetchRow();
		
		$pwd = md5($pwd.$rs['rand']);*/
		
		//if($rs['phone'] != "" && $pwd == $rs['passwd']){
			$url = "r=web/UpdatePwd";
			$vars = "PN=".$phone."&Passwd=".md5($password)."&Newpwd=".md5($new)."&Flag=6";
			$api = new userapi();
			$res = $api->curl_post($url, $vars);
			$res = json_decode($res);
			
			if($res->ret==0){
				return true;
			}
			return false;
			/*$new = md5(trim($new));
			$new = md5($new.$rs['rand']);
			
			$upArr = array('passwd'=>$new);
			$upRs = $this->updateData($upArr, "`phone` = '".$phone."'");
			
			if($upRs){
				return true;
			}*/
	//	}
		//return false;
		
		
	}
	/**
	 * 
	 * 重设密码
	 * @param $phone
	 * @param $new
	 */
	function resetPwd($phone,$new){
		
		$this->setMemberTable($phone);
		
		$sql = "select `phone`,`rand`,`passwd`  from `".$this->_name."` where `phone` = '".$phone."'";
		
		$rs = $this->query($sql)->fetchRow();
		
		$new = trim($new);
		$new = md5($new.$rs['rand']);
			
		if($rs['phone']){
			
			if($rs['passwd'] == $new){
				return true;
			}
			$upArr = array('passwd' => $new);
			$upRs = $this->updateData($upArr, "`phone` = '".$phone."'");
			
			if($upRs){
				Yii::app()->usermc2->delete($phone);
				return true;
			}
			return false;
		}
		return false;
		
		
	}
	/**
	 * 检查用户是否存在
	 * @param Int $phone
	 * @return Boolean (true|false)
	 */
	function existsUser($phone){
		$this->setMemberTable($phone);
		
		$sql = "select count(`id`) as total from `".$this->_name."` where `phone` = '".$phone."'";
		$rs = $this->query($sql)->fetchRow();
		
		if($rs['total']>0){
			return true;
		}
		return false;
	}
	/**
	 * 捡查用户是否是新用户
	 */
	function userIsNew($phone){
		$this->setPrefix('m_');
		
		$sql = "select count(`id`) as total from `".$this->_prefix."yeecard_order` where `phone` = '".$phone."'";
		$sql .= " and `pay_status` > 0";
		
		$rs = $this->query($sql)->fetchRow();
		
		if($rs['total'] > 0){
			return false;
		}
		return true;
	}
	/**
	 * 是否可以适合发送密码
	 */
	function isModify($phone){
		$this->setMemberTable($phone);
		$sql = "select count(`id`) as total from `".$this->_name."` where `phone` = '".$phone."' and `passwd` != ''";
		
		$rs = $this->query($sql)->fetchRow();
		//print_r($rs);exit;
		if($rs['total'] == 1){
			return true;
		}
		return false;
	}
	/**
	 * 
	 * 绑定手机号
	 * @param $phone
	 */
	function modifyPhone($phone,$oldphone){
		//modifytimes
		$this->setMemberTable($phone);
		$sql = "select `modifytimes` from `".$this->_name."` where `phone` = '".$oldphone."'";
		$rs = $this->query($sql)->fetchRow();
		if($rs['modifytimes'] > 0){
			//已经绑定过
			return 2;
		}
		//$sql = "update `".$this->_name."` set `phone` = '".$phone."',`modifytimes`='1' where `phone` = '".$oldphone."'";
		$upArr = array('phone'=>$phone,'modifytimes'=>'1');
		$upRs = $this->updateData($upArr, "`phone` = '".$oldphone."'");
		if($upRs){
			return 1;
		}
		return 3;
	}
	function initPwd($phone){
		$this->setMemberTable($phone);
		$sql = "select `passwd`,`rand`,`smstimes`,`update_date` from `".$this->_name."` where `phone` = '".$phone."'";
		$rs = $this->query($sql)->fetchRow();
		
		if(!empty($rs['passwd'])){
			return array('state'=>false,'content'=>3);
		}
		
		$times = $rs['smstimes']+1;
		
		$times = empty($times)?'1':$times;
		
		
		$pwd1 = rand(100000, 999999);
		$pwd = md5($pwd1);
		$pwd = md5($pwd.$rs['rand']);
		//$sql = "update `".$this->_name."` set `passwd` = '".$pwd."',`smstimes` = '".$times."',`update_date` = '".date('Y-m-d H:i:s')."'";
		//$sql .= " where `phone` = '".$phone."'";
		$upArr = array('passwd'=>$pwd,
				'smstimes'=>$times,
				'update_date'=>date('Y-m-d H:i:s')
		);
		$upRs = $this->updateData($upArr, "`phone` = '".$phone."'");
		
		if($upRs){
			//成功
			return array('state'=>true,'content'=>$pwd1);
		}
		//未知错误
		return array('state'=>false,'content'=>4);
	}
	/**
	 * 
	 */
	function __destruct() {
	
		//TODO - Insert your code here
	}
}

?>